package com.atguigu.day03;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;

//线程不安全
public class NotSafeDemo2 {

    public static void main(String[] args) {
//        List<String> list = new CopyOnWriteArrayList<>();//Collections.synchronizedList(new ArrayList<>());//new ArrayList<>();
//        Set<String> set =  new CopyOnWriteArraySet<>();//Collections.synchronizedSet(new HashSet<>());//new HashSet<>();
           //对于Hash来说，使用的不再是CopyOnWrite...，而是ConcurrentHashMap
        Map<String,String> map = new ConcurrentHashMap<>();//Collections.synchronizedMap(new HashMap<>());//new HashMap<>();
        for (int i = 0; i <100; i++) {
            new Thread(() -> {
                map.put(Thread.currentThread().getName(),UUID.randomUUID().toString().substring(1,7));
                System.out.println(map);
            }, String.valueOf(i)).start();
        }

    }
}
